<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="名称" prop="name">
      <el-input v-model="dataForm.name" placeholder=""></el-input>
    </el-form-item>
    <el-form-item label="编码" prop="code">
      <el-input v-model="dataForm.code" placeholder=""></el-input>
    </el-form-item>
    </el-form>
    <div style="padding-left:15px;">
      <code-selector ref="a" @selectChange="onChangeA"></code-selector>
      <code-selector ref="b" @selectChange="onChangeB"></code-selector>
      <code-selector ref="c" @selectChange="onChangeC"></code-selector>
      <code-selector ref="d" @selectChange="onChangeD"></code-selector>
      <code-selector ref="s" :maxlength="3"></code-selector>
      <code-selector ref="e" @selectChange="onChangeE"></code-selector>
      <code-selector ref="f"></code-selector>
    </div>
    <span slot="footer" class="dialog-footer">
      <el-checkbox v-model="series">连续添加</el-checkbox>
      <el-button  @click="visible = false">取消</el-button>
      <el-button type="primary" @click="createCode()">生成</el-button>
      <el-button type="primary" @click="dataFormSubmit()">保存</el-button>
    </span>
  </el-dialog>
</template>

<script>
  import CodeSelector from './code-selector'
  import {a0} from '@/catalog/a'

  export default {
    data () {
      return {
        visible: false,
        series: false,
        dataForm: {
          id: 0,
          name: '',
          code: ''
        },
        dataRule: {
          name: [
            { required: true, message: '名称不能为空', trigger: 'blur' }
          ],
          code: [
            { required: true, message: '编码不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    components: {
      CodeSelector
    },
    created () {

    },
    methods: {
      init (id) {
        this.dataForm.id = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.id) {
            this.$http({
              url: this.$http.adornUrl(`/generator/thing/info/${this.dataForm.id}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.name = data.thing.name
                this.dataForm.code = data.thing.code
              }
            })
          }
          this.$refs['a'].bindData(a0());
          this.$refs['b'].bindData([]);
          this.$refs['c'].bindData([]);
          this.$refs['d'].bindData([]);
          this.$refs['e'].bindData([]);
          this.$refs['f'].bindData([]);
        })
      },
      getMaxCode3(){
        this.$http({
          url: this.$http.adornUrl('/generator/thing/getMaxCode3'),
          method: 'get',
          params: this.$http.adornParams({
            'prefxCode': this.$refs['a'].getCode()+this.$refs['b'].getCode()+this.$refs['c'].getCode()+this.$refs['d'].getCode()
          })
        }).then(({data}) => {
          if (data && data.code === 0) {
            this.$refs['s'].putPlaceHolder(data.code3);
          }
        })
      },
      createCode () {
        let code = (this.$refs['a'].getCode()+this.$refs['b'].getCode()+this.$refs['c'].getCode()+this.$refs['d'].getCode()+this.$refs['s'].getCode()+this.$refs['e'].getCode()+this.$refs['f'].getCode());
        this.dataForm.code = code;
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/generator/thing/${!this.dataForm.id ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'id': this.dataForm.id || undefined,
                'name': this.dataForm.name,
                'code': this.dataForm.code
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = this.series
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      },
      onChangeA(pcode){
        console.log("changeA");
        let data = this.$refs.a.getItem().children;
        console.log(data);
        this.$refs.b.bindData(data);
      },
      onChangeB(pcode){
        let data1 = this.$refs['b'].getItem().children1;
        this.$refs['c'].bindData(data1);
        let data2 = this.$refs['b'].getItem().children2;
        this.$refs['e'].bindData(data2);
        this.$refs['f'].bindData([]);
        let data3 = this.$refs['b'].getItem().children3;
        this.$refs['s'].bindData(data3);
      },
      onChangeC(pcode){
        let data = this.$refs['c'].getItem().children;
        this.$refs['d'].bindData(data);
      },
      onChangeD(pcode){
        this.getMaxCode3();
      },
      onChangeE(pcode){
        let data = this.$refs['e'].getItem().children;
        this.$refs['f'].bindData(data);
      }
    }
  }
</script>
